package com.bocloud.cmp.server.controller.app.resource;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.app.resource.StatisticAppService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.ResultTools;

/**
 * @author Zaney
 * @data:2017年3月9日
 * @describe:页面数据统计控制层
 */
@RestController
@RequestMapping("/app/statistic")
public class StatisticAppController {

	@Autowired
	private StatisticAppService statisticAppService;

	/**
	 * 获取数据统计
	 *
	 * @return
	 */
	@RequestMapping(value = "/total", method = {RequestMethod.GET})
	public BsmResult total(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject object = JSONObject.parseObject(params);
		if (null != object) {
			return statisticAppService.total(object.getLong("id"));
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 获取资源列表
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/list", method = {RequestMethod.GET})
	public BsmResult listResource(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = true) String params) {
		List<Param> paramList = JSONObject.parseArray(params, Param.class);
		String resourceType = null;
		String resourceName = null;
		Long appId = null;
		Long clusterId = null;
		if (!ListTool.isEmpty(paramList)) {
			resourceType = (String) paramList.get(0).getParam().get("resourceType");
			resourceName = (String) paramList.get(0).getParam().get("resourceName");
			Object clusterIdObject = paramList.get(0).getParam().get("id");
			if (null != clusterIdObject && StringUtils.isNotBlank(clusterIdObject.toString())) {
				clusterId = Long.parseLong(paramList.get(0).getParam().get("id").toString());
			}
			Object appIdObject = paramList.get(0).getParam().get("appId");
			if (null != appIdObject && StringUtils.isNotBlank(appIdObject.toString())) {
				appId = Long.parseLong(paramList.get(0).getParam().get("appId").toString());
			}
			return statisticAppService.resourceList(resourceType, resourceName, page, rows, appId, clusterId);
		}
		return ResultTools.formatErrResult();
	}
	/**
	 * 获取节点下的pod
	 * @param page
	 * @param rows
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/node/list", method = {RequestMethod.GET})
	public BsmResult listNodePod(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = true) String params) {
		List<Param> paramList = JSONObject.parseArray(params, Param.class);
		String nodeName = null;
		Long clusterId = null;
		if (!ListTool.isEmpty(paramList)) {
			nodeName = (String) paramList.get(0).getParam().get("nodeName");
			Object clusterIdObject = paramList.get(0).getParam().get("id");
			if (null != clusterIdObject && StringUtils.isNotBlank(clusterIdObject.toString())) {
				clusterId = Long.parseLong(paramList.get(0).getParam().get("id").toString());
			}
			return statisticAppService.getNodePod(clusterId, nodeName);
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 获取资源详情
	 *
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/detail", method = {RequestMethod.GET})
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject object = JSONObject.parseObject(params);
		if (null != object) {
			return statisticAppService.detail(object.getString("resourceType"),
						object.getString("namespace"), object.getString("resourceName"), object.getLong("id"));
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 获取资源事件
	 */
	@RequestMapping(value = "/event", method = {RequestMethod.GET})
	public BsmResult event(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject object = JSONObject.parseObject(params);
		if (null != object) {
			return statisticAppService.event(object.getString("namespace"), object.getString("resourceType"),
						object.getString("resourceName"), object.getLong("id"));
		}
		return ResultTools.formatErrResult();
	}
	/**
	 * 获取日志信息
	 * @param params
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/log", method = {RequestMethod.GET})
	public BsmResult getLog(@RequestParam(value = Common.PARAMS, required = true) String params){
		JSONObject object = JSONObject.parseObject(params);
		if (null != object) {
			// 判断 当第一次没有集群存在的时候，会存在id为空的情况
			return statisticAppService.getLog(object.getString("namespace"), object.getString("resourceName"),
						object.getString("containerName"), object.getString("status"), object.getLong("id"));
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 获取游离pod
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/bare/pod", method = { RequestMethod.GET })
	public BsmResult getBarePod(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject object = JSONObject.parseObject(params);
		if (null != object) {
			return statisticAppService.getBarePod(Long.valueOf(object.getString("clusterId")),
					object.getString("nodeName"));
		}
		return ResultTools.formatErrResult();
	}

}
